﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.Physic;
using Engine.Audio;
using Engine.Entity;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Engine.Particles;


namespace Engine.SceneControl
{
    /// <summary>
    /// Descreve uma tela do Jogo
    /// </summary>
    public class SceneScreen : IScreen
    {
        #region properties

        private bool _isDebugDrawn = false;

        public bool isDebugDrawn
        {
            set
            {
                this._isDebugDrawn = value;
            }
            get
            {
                return this._isDebugDrawn;
            }
        }

        
        private IWorld _world = null;

        public IWorld World
        {
            set
            {
                this._world = value;
            }
            get
            {
                return this._world;
            }
        }

       

        #endregion

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            World.UpdateWorld(gameTime);

            foreach (ISoundEmitter item in _emiter)
            {
                item.setListenerPosition(World.Camera.Position);
            }

            foreach (IParticleEmitter item in _pemiter)
            {
                
                item.Update(gameTime);
            }


        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            
            World.DrawnWorld(gameTime);

            foreach (IParticleEmitter item in _pemiter)
            {
                item.setCamera(World.Camera);
                item.Draw(gameTime);
            }



            base.Draw(gameTime);
            
        }

        public override void LoadContent(EngineStuff es)
        {
            base.LoadContent(es);
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }
        

        private IList<ISoundEmitter> _emiter = new List<ISoundEmitter>();
        private IList<IParticleEmitter> _pemiter = new List<IParticleEmitter>();



        /// <summary>
        /// Emitter segue a posicao da Camera
        /// </summary>
        /// <param name="em"></param>
        public void AddParticleEmitter(IParticleEmitter em)
        {
            EntityMapper.getInstance().AddEntity(em);
            em.setEmitterPosition(Vector3.Zero);
            
            _pemiter.Add(em);
            //em.Play();

        }

        public void RemoveParticleEmitter(int id)
        {
            IEntity e = EntityMapper.getInstance().getEntity(id);
            EntityMapper.getInstance().RemoveEntity(e);
            
            _pemiter.Remove(e as IParticleEmitter);
        }

        public void RemoveParticleEmitter(IParticleEmitter e)
        {
            
            EntityMapper.getInstance().RemoveEntity(e);
            _pemiter.Remove(e);

        }




        /// <summary>
        /// Emitter segue a posicao da Camera
        /// </summary>
        /// <param name="em"></param>
        public void AddSoundEmitter(ISoundEmitter em)
        {
            EntityMapper.getInstance().AddEntity(em);
            em.setEmitterPosition(Vector3.Zero);
            em.setListenerPosition(new Vector3(1000,0,0));
            _emiter.Add(em);
            //em.Play();

        }

        public void RemoveSoundEmitter(int id)
        {
            IEntity e = EntityMapper.getInstance().getEntity(id);
            EntityMapper.getInstance().RemoveEntity(e);
            (e as ISoundEmitter).Stop();
            _emiter.Remove(e as ISoundEmitter);
        }

        public void RemoveSoundEmitter(ISoundEmitter e)
        {
            e.Stop();
            EntityMapper.getInstance().RemoveEntity(e);
            _emiter.Remove(e);

        }

        public void SoundEffectDistanceScale(float dist)
        {         
            SoundEffect.DistanceScale = dist;
        }
        public void SoundEffectMasterVolume(float vol)
        {
            SoundEffect.MasterVolume = vol;
        }


        
       
       

    }
}
